package com.wangnan.currentactivity.util;

import android.accessibilityservice.AccessibilityService;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import com.wangnan.currentactivity.BaseApplication;
import com.wangnan.currentactivity.ui.UserBillBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: ActivityUtil
 * @Description: Activity跳转工具类
 * @Author android张
 * @Date: 2020/1/25
 */

public class ActivityUtil {

    /**
     * 跳转系统悬浮窗授权页
     *
     * @param context
     */
    public static void turnToOverlayPermission(Context context) {
        // Android 6.0以上需要配置
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            intent.setData(Uri.parse("package:" + context.getPackageName()));
            context.startActivity(intent);
        }
    }

    /**
     * 跳转通知授权页
     *
     * @param context
     */
    public static void turnToNotifyPermission(Context context) {
        Intent intent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Android 8.0以上Intent跳转配置
            intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Android 5.0以上Intent配置
            intent = new Intent("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", context.getPackageName());
            intent.putExtra("app_uid", context.getApplicationInfo().uid);
        } else {
            // Android 5.0以下Intent配置
            intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        }
        context.startActivity(intent);
    }

    /**
     * 去授权"辅助服务"
     *
     * @param context
     */
    public static void toAuthAccessibilityService(Context context) {
        Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
        context.startActivity(intent);
    }


    /**
     * 通过文本查找节点（节点无id值时使用）
     */
    public static void   findNodeInfoByText(AccessibilityService service, String text, String desc, String className){
        Log.e("zzhdesc===", desc);
        AccessibilityNodeInfo  nodeInfo = service.getRootInActiveWindow();

        String A = "1";
        //text ?: desc
        if (text!= null) {
            A =   text;
        }else if (text  == null){
            A =   desc;
        }
        Log.e("zzh测试进入的A =", A);
        if (nodeInfo == null) {
            Log.e("zzh测试", "noteInfo is　null");
            return;
        } else {
            recycle(nodeInfo);
        }
        Log.e("zzh测试", "==============================================");
        List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText(A);

        for (int j = 0; j<list.size(); j++){
            if (list.get(j).getClassName().equals(className)){
                if (desc != null){
                    if (desc == list.get(j).getContentDescription().toString()) {
                        Rect  bounds = new Rect();
                        list.get(j).getBoundsInScreen(bounds);
                        return;
                    }
                }
            }else {
                Rect  bounds = new Rect();
                list.get(j).getBoundsInScreen(bounds);
                return;
            }
        }
        return ;

    }
    public static void recycle(AccessibilityNodeInfo info) {
        if (info.getChildCount() == 0) {
            Log.e("zzh测试", "child widget----------------------------" + info.getClassName());
            Log.e("zzh测试", "showDialog:" + info.canOpenPopup());
            Log.e("zzh测试", "Text：" + info.getText());
            Log.e("zzh测试", "windowId:" + info.getWindowId());
            Log.e("zzh测试", "CollectionInfo:" + info.getCollectionInfo());
//            Rect  bounds = new Rect();
//            Log.e("zzh测试", "BoundsInScreen:" + info.getBoundsInScreen(bounds)+"");
//            Log.e("zzh测试", "ContentDescription:" + info.getContentDescription());
//            Log.e("zzh测试", "ViewIdResourceName:" + info.getViewIdResourceName());
//            Log.e("zzh测试", "ActionList:" + info.getActionList());
//            Log.e("zzh测试", "Child(2):" + info.getChild(2));
//            Log.e("zzh测试", "ChildCount():" + info.getChildCount());
//            Log.e("zzh测试", "CollectionItemInfo():" + info.getCollectionItemInfo());
//            Log.e("zzh测试", "DrawingOrder():" + info.getDrawingOrder());
//            Log.e("zzh测试", "HintText():" + info.getHintText());
//            Log.e("zzh测试", "InputType():" + info.getInputType());
//            Log.e("zzh测试", "LabeledBy():" + info.getLabeledBy());
//            Log.e("zzh测试", "LabelFor():" + info.getLabelFor());
//            Log.e("zzh测试", "LiveRegion():" + info.getLiveRegion());
//            Log.e("zzh测试", "TextSelectionEnd():" + info.getTextSelectionEnd());
//            Log.e("zzh测试", "TextSelectionStart():" + info.getTextSelectionStart());
//            Log.e("zzh测试", "TraversalAfter():" + info.getTraversalAfter());
//            Log.e("zzh测试", "TraversalBefore():" + info.getTraversalBefore());
//            Log.e("zzh测试", "Window():" + info.getWindow());
//            Log.e("zzh测试", "Actions():" + info.getActions());
//            Log.e("zzh测试", "BoundsInParent():" + info.getBoundsInParent());

        } else {
            for (int i = 0; i < info.getChildCount(); i++) {
                if(info.getChild(i)!=null){
                    recycle(info.getChild(i));
                }
            }
        }
    }
    //为什么到这分开原因是以后不同数据处理不一样
    //支付宝账单详情
    public static void recycleZfbBillDetails(AccessibilityNodeInfo info) {
        if (info.getChildCount() == 0 && info.getText() != null) {
            if (info.getText().toString().replace(" ","").length() >= 2){
                BaseApplication.userDetailsList.add(new UserBillBean(info.getText().toString(),BaseApplication.A++));
                Log.e("zzh支付宝账单详情测试长度大于等于2", "Text："+ BaseApplication.userDetailsList.get(BaseApplication.A-1).getName()+"------"+
                        +BaseApplication.userDetailsList.get(BaseApplication.A-1).getSerialNumber());
            }
        } else {
            for (int i = 0; i < info.getChildCount(); i++) {
                if(info.getChild(i)!=null){
                    recycleZfbBillDetails(info.getChild(i));
                }
            }
        }
    }
    //微信账单详情
    public static void recycleWxBillDetails(AccessibilityNodeInfo info) {
        if (info.getChildCount() == 0 && info.getText() != null) {
            if (info.getText().toString().replace(" ","").length() >= 2){
                BaseApplication.userDetailsList.add(new UserBillBean(info.getText().toString(),BaseApplication.A++));
                Log.e("zzh微信账单详情测试长度大于等于2", "Text："+ BaseApplication.userDetailsList.get(BaseApplication.A-1).getName()+"------"+
                        +BaseApplication.userDetailsList.get(BaseApplication.A-1).getSerialNumber());
            }
        } else {
            for (int i = 0; i < info.getChildCount(); i++) {
                if(info.getChild(i)!=null){
                    recycleWxBillDetails(info.getChild(i));
                }
            }
        }
    }
    //微信红包详情
    public static void recycleWxLuckyMoney(AccessibilityNodeInfo info) {
        if (info.getChildCount() == 0 && info.getText() != null) {
            if (info.getText().toString().replace(" ","").length() >= 2){
                BaseApplication.userDetailsList.add(new UserBillBean(info.getText().toString(),BaseApplication.A++));
                Log.e("zzh微信红包详情测试长度大于等于2", "Text："+ BaseApplication.userDetailsList.get(BaseApplication.A-1).getName()+"------"+
                        +BaseApplication.userDetailsList.get(BaseApplication.A-1).getSerialNumber());
            }
        } else {
            for (int i = 0; i < info.getChildCount(); i++) {
                if(info.getChild(i)!=null){
                    recycleWxLuckyMoney(info.getChild(i));
                }
            }
        }
    }
    //云闪付账单详情
    public static void recycleYsfBillDetails(AccessibilityNodeInfo info) {
        if (info.getChildCount() == 0 && info.getText() != null) {
            if (info.getText().toString().replace(" ","").length() > 1){
                BaseApplication.userDetailsList.add(new UserBillBean(info.getText().toString(),BaseApplication.A++));
                Log.e("zzh云闪付账单详情测试长度大于等于2", "Text："+ BaseApplication.userDetailsList.get(BaseApplication.A-1).getName()+"------"+
                        +BaseApplication.userDetailsList.get(BaseApplication.A-1).getSerialNumber());
            }
        } else {
            for (int i = 0; i < info.getChildCount(); i++) {
                if(info.getChild(i)!=null){
                    recycleYsfBillDetails(info.getChild(i));
                }
            }
        }
    }
    //美团账单详情
    public static void recycleMtBillDetails(AccessibilityNodeInfo info) {
        if (info.getChildCount() == 0 && info.getText() != null) {
            if (info.getText().toString().replace(" ","").length() > 1){
                BaseApplication.userDetailsList.add(new UserBillBean(info.getText().toString(),BaseApplication.A++));
                Log.e("zzh美团账单详情测试长度大于等于2", "Text："+ BaseApplication.userDetailsList.get(BaseApplication.A-1).getName()+"------"+
                        +BaseApplication.userDetailsList.get(BaseApplication.A-1).getSerialNumber());
            }
        } else {
            for (int i = 0; i < info.getChildCount(); i++) {
                if(info.getChild(i)!=null){
                    recycleMtBillDetails(info.getChild(i));
                }
            }
        }
    }




    //清空全局数据
    public static void globalDeleteClearlist() {

        // 使用倒序循环删除
        for (int i1 = BaseApplication.userDetailsList.size()-1; i1>=0; i1--) {
            BaseApplication.userDetailsList.remove(i1);
            BaseApplication.A =  0;
            BaseApplication.isDetail=  0;
        }

    }

    //判断是否进入的是详情
    public static  void isDetail(AccessibilityNodeInfo info){

        if (info.getChildCount() == 0 && info.getText() != null) {
            if (info.getText().toString().replace(" ","").length() > 1){
                if (info.getText().toString().equals("账单详情")){
                   BaseApplication.isDetail = 1;
                   return;
                }else if (info.getText().toString().equals("交易详情")){
                    BaseApplication.isDetail = 2;
                    return;
                } else  if (info.getText().toString().equals("详情")) {
                    BaseApplication.isDetail = 0;
                    BaseApplication.isFistMt = 0;
                } else if (info.getText().toString().equals("账单")){
                    BaseApplication.isDetail = 0;
                    BaseApplication.isFistWx = 0;
                }
            }
        } else {
            for (int i = 0; i < info.getChildCount(); i++) {
                if(info.getChild(i)!=null){
                    isDetail(info.getChild(i));
                }
            }
        }
    }


    /**
     *   生成主键(19位数字)
     *  主键生成方式,年月日时分秒毫秒的时间戳 例如：1810311557430000845
     */
    public static synchronized Long getUUIDTOLongNew(){
        try {
            TimeUnit.NANOSECONDS.sleep(1000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String msg="";
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSSSSSS"); //1810311557430000845               
        //        msg = sdf.format(date).substring(2);
        return Long.parseLong(sdf.format(date));
    }


}
